using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Diagnostics;
using System.Windows.Forms;

namespace Janus.NorthwindDemo2007
{
	public class SuppliersDataProvider : DataProvider
	{



		internal Janus.NorthwindDemo2007.NorthwindDataTableAdapters.SuppliersTableAdapter SuppliersTableAdapter1;
		internal Janus.NorthwindDemo2007.NorthwindDataTableAdapters.Suppliers_ProductsTableAdapter SuppliersProductsAdapter;

		protected override bool FillCore()
		{
			this.SuppliersTableAdapter1.Fill(this.DataSet.Suppliers);
			this.SuppliersProductsAdapter.Fill(this.DataSet.Suppliers_Products);
			return true;
		}
		protected override bool UpdateCore()
		{
			this.SuppliersTableAdapter1.Update(this.DataSet.Suppliers);
			this.SuppliersProductsAdapter.Update(this.DataSet.Suppliers_Products);
			return true;
		}
		protected override bool UpdateDataRowCore(System.Data.DataRow row)
		{
			this.SuppliersTableAdapter1.Update(row);
			this.UpdateProducts(row);
			return true;
		}

		public SuppliersDataProvider() : base()
		{
			this.SuppliersTableAdapter1 = new Janus.NorthwindDemo2007.NorthwindDataTableAdapters.SuppliersTableAdapter();
			this.SuppliersTableAdapter1.ClearBeforeFill = true;
			this.SuppliersProductsAdapter = new Janus.NorthwindDemo2007.NorthwindDataTableAdapters.Suppliers_ProductsTableAdapter();
			this.SuppliersProductsAdapter.ClearBeforeFill = true;
			this.DataAdapter = this.SuppliersTableAdapter1.DataAdapter;
		}
		private void UpdateProducts(DataRow row)
		{
			NorthwindData ds = null;
			ds = (NorthwindData)row.Table.DataSet;
			DataTable changes = ds.Suppliers_Products.GetChanges();
			if (changes != null)
			{
				foreach (DataRow detail in changes.Rows)
				{
					if (detail.RowState == DataRowState.Deleted)
					{
						if (detail.HasVersion(DataRowVersion.Original))
						{
							//find the row in the loaded data and delete it from the dataset too.
							DataRow drToDelete = this.DataSet.Suppliers_Products.FindBySupplierIDProductID((int)detail["SupplierID", DataRowVersion.Original], (int)detail["ProductId", DataRowVersion.Original]);
							if (drToDelete != null)
							{
								drToDelete.Delete();
								drToDelete.AcceptChanges();
							}
						}
					}
					else
					{
						//reload the row in the main dataset 
						this.DataSet.Suppliers_Products.LoadDataRow(detail.ItemArray, true);
					}
				}
				this.SuppliersProductsAdapter.Update((NorthwindData.Suppliers_ProductsDataTable)changes);
			}
		}
		protected override void OnRowUpdated(System.Data.OleDb.OleDbRowUpdatedEventArgs e)
		{
			//this is done with tables that have autonumber fields
			if (e.Status != UpdateStatus.ErrorsOccurred)
			{
				// Include a variable and a command to retrieve the identity value from the Access database.
				int newID = 0;
				System.Data.OleDb.OleDbCommand idCMD = null;

				if (e.StatementType == StatementType.Insert)
				{
					idCMD = new System.Data.OleDb.OleDbCommand("SELECT @@IDENTITY", this.SuppliersTableAdapter1.Connection);
					// Retrieve the identity value and store it in the CategoryID column.
					newID = System.Convert.ToInt32(idCMD.ExecuteScalar());
					e.Row["SupplierID"] = newID;
					idCMD.Dispose();
				}
			}
		}

	}

} //end of root namespace